package stack;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class test {
    public static void main(String[] args) {
        Deque<Integer> deque = new LinkedList<>();//双端队列
        Queue<Integer> queue = new LinkedList<>();//队列
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);

        System.out.println(queue.poll());//队列先进先出
        System.out.println(queue.peek());//队列是尾插法
    }
    public static void main2(String[] args) {
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(1);
        stack.push(2);
        System.out.println(stack.peek());
    }
    public static void main1(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(5);

        Integer x = stack.pop();//获取栈顶元素并删除最上面的元素
        System.out.println(x);

        int ret = stack.peek();//获取栈顶元素但不删除
        System.out.println(ret);
    }

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        //如果为空或者括号的总数量是奇数，无效
        if(s == null || s.length() % 2 != 0) {
            return false;
        }
        //是左括号就先入栈
        for(char c : s.toCharArray()) {
            if(c == '(') {
                stack.push(')');
            } else if (c == '{') {
                stack.push('}');
            } else if (c == '[') {
                stack.push(']');
            } else if (stack.isEmpty() || stack.pop() != c) {//如果是右括号就看栈是否为空或者栈顶是否匹配
                return false;
            }
        }
        return stack.isEmpty();
    }
}
